#!/bin/sh

if [ "$EUID" != "0" ]; then echo "Must be run as root"; exit 1; fi

##################################################################
# The following perl function kills a process by the passed name
# it returns 0 if it killed a process, 1 otherwise
#
function kill_damoen_proc
{
perl -e '
	$parent_id=$ARGV[0];
	open(PS,qq{ps -ax |});
	
	foreach (<PS>)
	{
		if ($_ =~ /aksusbd/ && $_ !~ /$parent_id/)
		{
			s/^\s+|\s+$//g;
			@proc = split /\s+/, $_, 2;
			kill 9, $proc[0];
			print qq{Process $proc[0] stopped.\n};
			last;	
		}
	}
	close PS;
	' $1
}


##################################################################
# Path and other definitions
#
oldStartupFolder=/System/Library/StartupItems/Aladdin
startupFolder=/Library/StartupItems/Aladdin
launchdFolder=/Library/LaunchDaemons

driver_label=com.aladdin.aksusbd

driver_plist="$driver_label".plist

startup_plist=StartupParameters.plist
startup_item=Aladdin

usb_daemon=aksusbd
daemon_path=/usr/libexec

resdir="$1"/Contents/Resources

##################################################################
# incidently old versions of the installer put the StartUpItem
# to System/Library/... instead of /Library as documented by Apple
# clean up old and deprecated startup items
# if the old startup item exists we remove it
#
if [ -e "$oldStartupFolder" ]; then 
	rm -rf "$oldStartupFolder"
fi 

##################################################################
# make sure the daemon has correct ownership and permissions
#
chmod 755 "$daemon_path/$usb_daemon"
chown root:admin "$daemon_path/$usb_daemon"

##################################################################
# now we get the major version of the OS
#
sysv=`perl -e '@v = split /\./, $ARGV[0]; print $v[0];' \`uname -r\``

# now we need to work out which start up mechanism to use:
# on systems prior to 10.4 startup item 
# on 10.4 and up launchd plist files
#
if [ $sysv -lt 8 ]; then

	# check that StartupItems folder exists, and create it if not
	# also create intermediary folders
	#
	if [ ! -d "$startupFolder" ]; then 
		mkdir -p "$startupFolder"
	fi
	
	#first we kill daemons that may be running
	#
	kill_daemon_proc $$

	#now we copy the StartupItem
	#
	cp "$resdir/$startup_plist" "$startupFolder"
	cp "$resdir/$startup_item"  "$startupFolder"

	# now we make sure, file permissions and ownerships are correct
	#
	chmod -R 755 "$startupFolder"
	chown -R root:admin "$startupFolder"
	sync
	
	#finally start the daemons by invoking the startup item
	#
	"$startupFolder/$startup_item"
	
else
	
	# if the startup item exists we remove it
	#
	if [ -e "$startupFolder" ]; then 
		rm -rf "$startupFolder"
	fi 
	
	# check if LaunchDaemons folder exists
	#
	if [ -e "$launchdFolder" ]; then
	
		# now we should unload the plist if the job is running
		#
		if [ "`launchctl list | grep $driver_label`" ]; then 
			launchctl unload "$launchdFolder/$driver_plist"
		fi
	else		
		
		mkdir "$launchdFolder"
		chmod 755 "$launchdFolder"
		chown root:admin "$launchdFolder"
	
	fi 
	
	#now we can kill daemons that may be running
	#
	kill_damoen_proc $$

	#now we copy the launchd plists
	#
	cp "$resdir/$driver_plist" "$launchdFolder"
	chown root:admin "$launchdFolder/$driver_plist"
	
	#finally we start the daemons by loading the plists
	#
	launchctl load "$launchdFolder/$driver_plist"

fi

exit 0
